package com.itita.ww2.model.user.battle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.itita.ww2.model.user.UserProfile;

public class BaseBattleState {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(BaseBattleState.class);
	// 
	protected UserProfile userProfile;
	protected BattleType battleType;
	protected ISFSObject lastDeployArmy; // 最近一次开始战斗派遣的兵
	protected ISFSObject lastBattleArmy; // 最近一次战斗后的兵
	protected boolean offlineSubArmy; // 掉线扣兵

	public BaseBattleState(UserProfile userProfile) {
		this.userProfile = userProfile;
		this.battleType = BattleType.None;
		this.offlineSubArmy = true;
	}

	public static enum BattleType {
		None, // 不在战斗 
		Mission, // 副本
		WorldBattle, // 世界地图中打据点
		AlBattle, // 盟战打据点
		AGGRESSION, // 离线作战
	}

	public void startBattle() {
		// logger.debug("user: {}:{} {} start!", userProfile.getNickname(), userProfile.getUid(), battleType.name());
	}

	public void endBattle(boolean online, boolean victory) {
		if (online) {
			deductBattleArmy();
		} else if (offlineSubArmy) {
			deductDeployArmy();
		}
	}

	protected void deductDeployArmy() {
		// 扣除派遣的兵
		if (lastDeployArmy != null) {
			for (String item : lastDeployArmy.getKeys()) {
				// 死亡数量
				int count = lastDeployArmy.getSFSObject(item).getInt("Total");
				userProfile.getArmyManager().armyCountOper(item, count, false);
			}
			userProfile.getArmyManager().update();
			userProfile.getArmyManager().syncArmy();
		}
	}

	protected void deductBattleArmy() {
		// 扣除战斗后的兵
		if (lastBattleArmy != null) {
			for (String item : lastBattleArmy.getKeys()) {
				// 死亡数量
				int count = lastBattleArmy.getSFSObject(item).getInt("Dead");
				userProfile.getArmyManager().armyCountOper(item, count, false);
			}
			userProfile.getArmyManager().update();
			userProfile.getArmyManager().syncArmy();
		}
	}

	public BattleType getBattleType() {
		return battleType;
	}

	public void setOfflineSubArmy(boolean offlineSubArmy) {
		this.offlineSubArmy = offlineSubArmy;
	}

	public ISFSObject getLastDeployArmy() {
		return lastDeployArmy;
	}

	public void setLastDeployArmy(ISFSObject lastDeployArmy) {
		this.lastDeployArmy = lastDeployArmy;
	}

	public ISFSObject getLastBattleArmy() {
		return lastBattleArmy;
	}

	public void setLastBattleArmy(ISFSObject lastBattleArmy) {
		this.lastBattleArmy = lastBattleArmy;
	}
}
